package com.zzyl.controller;

/**
 * @Description RoleController
 * @Author xxxx
 * @Date 2024-10-24
 */

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 角色前端控制器
 */
@Slf4j
@Api(tags = "角色管理")
@RestController
@RequestMapping("role")
//构造器注入：好处：创建当前对象的时候直接给属性注入数据，spring官方推荐，但是不可以用在junit单元测试中
//实现步骤：1.在类上使用@RequicredArgsConstructor(lombok)
//        2.属性要使用final修饰
//底层原理：编译是会自动生成构造器 public RoleController（final修饰的属性类型参数1，final修饰的属性类型参数2...）
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;

    /**
     * 角色分页查询
     * @param roleDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("page/{pageNum}/{pageSize}")
    @ApiOperation("角色分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "roleDto"),
            @ApiImplicitParam(paramType = "path",name = "pageNum",value = "页码",example = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "path",name = "pageSize",value = "每页条数",example = "10",dataType = "Integer")
    })
    @ApiOperationSupport(includeParameters = {"roleDto.roleName"})
    public ResponseResult<PageResponse<RoleVo>> findRoleVoPage(
            @RequestBody RoleDto roleDto,
            @PathVariable("pageNum") int pageNum,
            @PathVariable("pageSize") int pageSize) {
        log.info("角色分页，请求参数：roleDto={}，pageNum={}，pageSize={}",roleDto,pageNum,pageSize);

        //调用业务获取分页数据
        PageResponse<RoleVo> pageResponse = roleService.findRoleVoPage(roleDto,pageNum,pageSize);
        return ResponseResult.success(pageResponse);
    }

    /**
     *  保存角色
     * @param roleDto 角色DTO对象
     * @return RoleVo
     */
    @PutMapping
    @ApiOperation("角色添加")
    @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "roleDto")
    @ApiOperationSupport(includeParameters = {"roleDto.roleName","roleDto.dataState"})
    public ResponseResult<Boolean> createRole(@RequestBody RoleDto roleDto) {
        log.info("角色添加，请求参数：{}",roleDto);

        //调用业务实现角色添加
        roleService.createRole(roleDto);

        //返回
        return ResponseResult.success();
    }

    /**
     * 根据角色查询选中的资源数据
     * @param roleId
     * @return
     */
    @ApiOperation(value = "根据角色查询选中的资源数据")
    @GetMapping("/find-checked-resources/{roleId}")
    public ResponseResult<Set<String>> findCheckedResources(@PathVariable("roleId") Long roleId){

        log.info("根据角色查询选中的资源数据，请求参数：roleId={}",roleId);

        //调用业务获取指定角色的关联资源编号集合
        Set<String> resourceNoSet = roleService.findCheckedResources(roleId);
        //返回数据
        return ResponseResult.success(resourceNoSet);
    }

    /**
     * 角色修改
     * @param roleDto
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "角色修改",notes = "角色修改")
    @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "roleDto")
    @ApiOperationSupport(includeParameters = {"roleDto.roleName","roleDto.dataState","roleDto.dataScope","roleDto.checkedResourceNos","roleDto.checkedDeptNos","roleDto.id"})
    public ResponseResult<Boolean> updateRole(@RequestBody RoleDto roleDto) {

        log.info("角色修改,请求参数：{}",roleDto);

        //调用业务修改角色信息
        roleService.updateRole(roleDto);

        //返回数据
        return ResponseResult.success(true);
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @ApiOperation("删除角色")
    @DeleteMapping("/{roleId}")
    public ResponseResult remove(@PathVariable("roleId") Long roleId) {
        log.info("删除角色，请求参数：roleId={}",roleId);

        //调用业务删除角色相关信息
        roleService.remove(roleId);

        //返回
        return ResponseResult.success();
    }

    /**
     * 角色下拉框
     * @return
     */
    @ApiOperation("角色下拉框")
    @PostMapping("/init-roles")
    public ResponseResult roleDropdownMenu(){
        List<RoleVo> roleVo = roleService.roleDropdownMenu();
        return ResponseResult.success(roleVo);
    }
}
